package org.jeecg.modules.relation.relclssubtearoom.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.common.api.IRow2ColTransfer;
import org.jeecg.modules.common.baseentity.Row2ColModel;
import org.jeecg.modules.common.basevo.ColumnsVo;
import org.jeecg.modules.common.utils.BeanObjUtil;
import org.jeecg.modules.relation.relclassstudent.vo.RelClassesAndStudentsVO;
import org.jeecg.modules.relation.relclssubtearoom.entity.RelClassSubjectTeacher;
import org.jeecg.modules.relation.relclssubtearoom.mapper.RelClassSubjectTeacherMapper;
import org.jeecg.modules.relation.relclssubtearoom.service.IRelClassSubjectTeacherService;
import org.jeecg.modules.relation.relclssubtearoom.vo.RelClassSubjectTeacherVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: rel_class_subject_teacher
 * @author: baochangrong
 * @Date: 2022-02-16
 * @Version: V1.0
 */
@Service
public class RelClassSubjectTeacherServiceImpl extends ServiceImpl<RelClassSubjectTeacherMapper, RelClassSubjectTeacher> implements IRelClassSubjectTeacherService {

    @Autowired
    private RelClassSubjectTeacherMapper relClassSubjectTeacherMapper;
    @Autowired
    private IRow2ColTransfer row2ColTransfer;

    @Override
    @Transactional
    public IPage<RelClassSubjectTeacherVO> queryAllList(Page<RelClassSubjectTeacherVO> page,RelClassSubjectTeacher relClassSubjectTeacher, String tenantId) {
         List<RelClassSubjectTeacherVO> relClassSubjectTeacherVOList = relClassSubjectTeacherMapper.queryAllList( relClassSubjectTeacher,tenantId);
         return page.setRecords(relClassSubjectTeacherVOList);
    }

    @Override
    @Transactional
    public List<JSONObject> queryPageListPro(Page<RelClassSubjectTeacherVO> page,RelClassSubjectTeacher relClassSubjectTeacher,Map<String,String> queryParam) throws NoSuchFieldException, IllegalAccessException {
        List<RelClassSubjectTeacherVO> relClassSubjectTeacherVOList =  relClassSubjectTeacherMapper.queryAllList(relClassSubjectTeacher,relClassSubjectTeacher.getTenantId());

//        String[] groupFields = new String[]{"classId"}; //固定分组列
//        String[] colsFields = new String[]{"subjectId"}; //列名属性所在的属性：例如：科目字段（值是=语文 英语 数学）
//        String[] valueFields = new String[]{"teacherId"}; //允许空，值属性所在的属性：例如：成绩字段（值是=98.5 99 59.5）


        // Liss<Object> 无法接收 List<Vo>对象
//        List<JSONObject> dataList = BeanObjUtil.dataRow2ColData(relClassSubjectTeacherVOList,groupFields,colsFields,valueFields);

        Class clazzT = RelClassSubjectTeacherVO.class;
        //BeanObjUtil.getFieldByName()
        String[] groupFields =new String[]{"classId" ,"className" };// new String[]{"classId"}; //1.固定分组属性名称
        String[] groupFieldsAsName = new String[]{"classId","className"};             //2.固定分组属性显示别名:必须返回id主键属性
        String[] row2colsKeyFields = new String[]{"subjectId"}; //科目列 //3.行转列动态列对应的关键属性字段
        Map<String,String>[] row2colsKeyFieldsDict = null ;
        String[] newColsValueByFields = new String[]{"teacherId" ,"teacherName","classroomId" ,"classroomName","id","classId","subjectId"};  //4.行转列动态列对应的值字段, 优良率数值取值字段
        String[] newColsValueUnitSymboles =  null;//new String[]{"%","人",""};            //5.动态列对应的单位符号，允许空，例如：“%” “人” “个”等
        String[] groupCalculationFun = null;//new String[]{"sum","count"} ;                          //6.聚聚合计算函数，支持[sum,count,max,min]不区分大小写
        String[] groupCalculationFunForValueField = null;//new String[]{"analysisYlrs","analysisYlv"};              //7.聚合函数作用字段，必须是newColsValueByFields中的字段不能是别的字段
        String[][] groupCalculationAsColName = null;//new String[][]{{"人数总和","人数数量"},{"率和","率量"}};                          //8.计算值列名字符串=列名+字符串，值=“Calculation”，所得列名=“语文Calculation”
        String newValueWeightCoefficientFieldes = null; //"classStudentAllCount"; /*9.聚合函数使用的权重系数字段,*/
        String newWeightCoefficientAsColName = null;//"权重系数"; //10.权重系数列名

        Row2ColModel row2ColModel = new Row2ColModel(clazzT,groupFields,groupFieldsAsName,row2colsKeyFields,row2colsKeyFieldsDict,newColsValueByFields,newColsValueUnitSymboles,groupCalculationFun,groupCalculationFunForValueField,groupCalculationAsColName,newValueWeightCoefficientFieldes,newWeightCoefficientAsColName);
        List<JSONObject> dataList =  row2ColTransfer.Row2ColToListJson(relClassSubjectTeacherVOList,row2ColModel);

        return dataList;
    }
    /**
     * 查询老师-科目-班级-教室的分配关系，应用与科目-老师的联动下拉框
     * @param relClassSubjectTeacherVO
     * @param queryParam,其中querySubjectSysDictFlag 是否需要查询科目的字典码：例如课间操、眼保健操等字典码
     * @return
     */
    @Transactional
    public List<RelClassSubjectTeacherVO> queryTeacherClassListBySubject(@Param("relClassSubjectTeacherVO") RelClassSubjectTeacherVO relClassSubjectTeacherVO, @Param("queryParam") Map<String,String> queryParam){
        /*默认查询：是否查询科目的系统字典数据 ：1是，其他的都是否，默认查询字典数据，方便在编辑课表、分配班级的任教老师和教室场地时候使用 */
        if(oConvertUtils.isEmpty(queryParam) ){
            queryParam = new HashMap<String,String>();
            queryParam.put("querySubjectSysDictFlag","1");
        }
        else if(oConvertUtils.isEmpty(queryParam.get("querySubjectSysDictFlag"))){
            queryParam.put("querySubjectSysDictFlag","1");
        }
        return relClassSubjectTeacherMapper.queryTeacherClassListBySubject(relClassSubjectTeacherVO,queryParam);
    };

    /**
     * 动态查询页面显示的列：科目列，动态查询显示
     * @param relClassSubjectTeacherVO
     * @param queryParam
     * @return
     */
    @Transactional
    public List<ColumnsVo> getColumnsList(@Param("relClassSubjectTeacherVO") RelClassSubjectTeacherVO relClassSubjectTeacherVO, @Param("queryParam") Map<String,String> queryParam){
        // 此方法应该放在科目subject中，参数增加：返回名称/简称   id或code 等参数设置。另外在班级clazz 中也增加动态列展示，老师teacher中也增加动态列展示，教室中增加动态列展示
        /*默认查询：是否查询科目的系统字典数据 ：1是，其他的都是否，默认查询字典数据，方便在编辑课表、分配班级的任教老师和教室场地时候使用 */
        if(oConvertUtils.isEmpty(queryParam) ){
            queryParam = new HashMap<String,String>();
            queryParam.put("querySubjectSysDictFlag","1");
        }
        else if(oConvertUtils.isEmpty(queryParam.get("querySubjectSysDictFlag"))){
            queryParam.put("querySubjectSysDictFlag","1");
        }
        return relClassSubjectTeacherMapper.getColumnsList(relClassSubjectTeacherVO,queryParam);
    };

    public int insertUpgradeRelClassSubjectTeacher(RelClassSubjectTeacherVO relClassSubjectTeacherVO){
        return relClassSubjectTeacherMapper.insertUpgradeRelClassSubjectTeacher(relClassSubjectTeacherVO);
    }

}
